package crawler;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Crawler implements Runnable {
	
	WebList visitedWebs;
	String uri;
	NVQueue<String> pendingWebs;
	NVQueue<String> emailList;
	
	
	int i;
	URL url;
	
	Matcher matcher;
	BufferedReader inputStream;
	String line;
	String href;
	
	Pattern emailPattern;
	Pattern hrefPattern;
	Pattern externalPattern;
	
	Matcher externalMatcher;
	
	CTSPipe cts;
	STCPipe stc;
	
	int maxPendingWebs;
	
	
	
	public Crawler(CTSPipe _cts, STCPipe _stc, String _uri) {
		uri = _uri;
		i = 0;
		cts = _cts;
		stc = _stc;
		maxPendingWebs = 10;
	}
	
	public void run() {
		try {
			//System.out.println(Thread.currentThread());
			emailList = new NVQueue<String>();
			pendingWebs = new NVQueue<String>();
			while (i<10) {
				System.out.println("Analizando URL: " + uri);
			
				url = new URL(uri);
				
		
				inputStream = new BufferedReader(new InputStreamReader(url.openStream()));
	
				emailPattern = Pattern.compile("[a-zA-Z0-9\\-]+@([a-zA-Z0-9]+\\.)+([a-zA-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)");
				hrefPattern = Pattern.compile("href=\"([^@\\s]+)\"");
				externalPattern = Pattern.compile("\\.([a-zA-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\\b");//(\\s|\\.|/)
				while ((line = inputStream.readLine()) != null) {
					matcher = emailPattern.matcher(line);
					while (matcher.find()) {
						emailList.push(matcher.group());
					}
				
					matcher = hrefPattern.matcher(line);
					while (matcher.find()) {
						href = matcher.group(1);
						
					
						externalMatcher = externalPattern.matcher(href);
						if (externalMatcher.find()) {
							pendingWebs.push(href);
						}
					}
				
				}
				inputStream.close();
				
				System.out.println("Cliente: " + pendingWebs.size());
				
				if (pendingWebs.size() > maxPendingWebs) {
					System.out.println("enviando al servidor");
					NVQueue<String> pendingWebsToSend = new NVQueue<String>();
					while (pendingWebs.size() > (maxPendingWebs/2)) {
						pendingWebsToSend.push(pendingWebs.pop());
					}
					pendingWebsToSend.mostrar("Webs a enviar");
					cts.sendPendindgWebs(pendingWebsToSend);
				} else if ((uri = pendingWebs.pop()) == null) {
					pendingWebs.join(stc.receivePendingWebs());

					//System.out.println("========PIDIENDO WEBS....============");
					pendingWebs.mostrar("Webs recibidas desde el controlador");
				}
				uri = pendingWebs.pop();
				
				i++;
			}
//			emailList.mostrar();
//			pendingWebs.mostrar();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
